package com.open.capacity.forum.controller;

import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.forum.dto.ResourcesFileDto;
import com.open.capacity.forum.model.ResourcesFile;
import com.open.capacity.forum.service.ResourcesBuyRecordService;
import com.open.capacity.forum.service.ResourcesFileService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author 蒋鑫
 * @version 创建时间 2021/3/1
 * 资源文件
 */
@RestController
@RequestMapping("/resources")
public class ResourcesFileController {
    @Resource
    private ResourcesFileService resourcesFileService;

    @Resource
    private ResourcesBuyRecordService resourcesBuyRecordService;

    /**
     * 文件资源上传
     */
    @PostMapping("/upload")
    public Result uploadResource(MultipartFile file, @RequestParam Map<String, Object> resourcesFile) throws Exception {
        if (file == null || file.getOriginalFilename() == null || "".equalsIgnoreCase(file.getOriginalFilename().trim())) {
            return Result.failed("文件为空");
        }
        return resourcesFileService.uploadResources(file, resourcesFile);
    }

    /**
     * 查询自己上传的全部资源（不包括删除掉的资源）
     */
    @GetMapping("/selectAllResources")
    public PageResult<ResourcesFile> selectAllResources(@RequestParam Map<String, Object> params) {
        try {
            return resourcesFileService.selectAllResources(params);
        } catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 修改资源相关信息
     */
    @PostMapping("/updateResource")
    public Result updateResource(@RequestBody ResourcesFile resourcesFile) throws ControllerException {
        return resourcesFileService.updateResource(resourcesFile);
    }

    /**
     * 删除自己上传的资源
     */
    @PostMapping("/delResources")
    public Result delResources(@RequestBody ResourcesFile resourcesFile) {
        try {
            boolean result = resourcesFileService.removeById(resourcesFile.getFileid());
            if (result) {
                return Result.succeed("删除成功");
            } else {
                return Result.failed("删除失败");
            }

        } catch (ControllerException e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 查询全部上传资源
     */
    @GetMapping("/selectAll")
    public PageResult<ResourcesFileDto> selectAll(ResourcesFileDto resourcesFileDto) {
        try {
            return resourcesFileService.selectAll(resourcesFileDto);
        } catch (ControllerException e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 下载资源
     */
    @PostMapping("/downloadResource")
    public Result downloadResource(@RequestBody Map<String, Object> map) {
        try {
            return resourcesBuyRecordService.buyResource(map);
        } catch (ControllerException e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 下载成功后生成下载记录，并且更新下载称号
     */
    @PostMapping("/buyRecords")
    public Result buyRecords(@RequestBody Map<String, Object> map) {
        try {
            return resourcesBuyRecordService.buyRecords(map);
        } catch (ControllerException e) {
            throw new ControllerException(e);
        }
    }
}
